/**
 * ---------------------------
 * Time: 2017/8/14 18:43.
 * Author: Cangshi
 * View: http://palerock.cn
 * ---------------------------
 */
define(['app', 'zepto'], function (app, $) {
    app.controller('detail', function ($scope, $routeParams, deviceService, MaintainPlan) {
        var deviceIndex;
        var init = function () {
            if ($routeParams.deviceIndex == null || Number.isNaN(parseInt($routeParams.deviceIndex))) {
                app.backEvent();
                return;
            }
            deviceIndex = $routeParams.deviceIndex;
            $scope.deviceIndex = deviceIndex;
            $scope.thisDevice = $scope.devices[deviceIndex];
            if (!$scope.thisDevice) {
                app.backEvent();
            }
        };
        init();

        // 添加设备运行时间
        $scope.runDevice = function () {
            $.prompt('添加设备运行的时间（单位：h）',
                function (value) {
                    var recordChangedDuration = parseInt(value);
                    if (!recordChangedDuration) {
                        $.alert('请正确的输入一个数字！');
                        return;
                    }
                    $scope.thisDevice.addRunningRecord(recordChangedDuration);
                    $scope.saveEdit('添加运行记录成功');
                }
            );
        };

        // 执行保养
        $scope.doMaintain = function (index) {
            $.confirm('确定执行[' + $scope.thisDevice.maintainPlans[index].maintainName + ']？',
                function () {
                    var maintainPlan = $scope.thisDevice.maintainPlans[index];
                    maintainPlan.takeMaintain($scope.thisDevice);
                    $scope.saveEdit('进行' + maintainPlan.maintainName + '成功');
                });
        };

        // 修改保养计划所限制的运行时长
        $scope.modifyPlansDuration = function (index) {
            var thisPlan = $scope.thisDevice.maintainPlans[index];
            $.prompt('修改' + thisPlan.maintainName + '所限制的运行时长[原来:'
                + thisPlan.limitingConditionFromRunningDuration + ']（单位：h）',
                function (value) {
                    var limitingConditionFromRunningDuration = parseInt(value);
                    if (limitingConditionFromRunningDuration == null && Number.isNaN(limitingConditionFromRunningDuration)) {
                        $.alert('请正确的输入一个数字！');
                        return;
                    }
                    thisPlan.limitingConditionFromRunningDuration = limitingConditionFromRunningDuration;
                    $scope.saveEdit('修改' + thisPlan.maintainName + '所限制的运行时长为:' + limitingConditionFromRunningDuration + '成功');
                }
            );
        };

        // 修改保养计划所限制的运行天数
        $scope.modifyPlansDaysNumber = function (index) {
            var thisPlan = $scope.thisDevice.maintainPlans[index];
            $.prompt('修改' + thisPlan.maintainName + '所限制的运行天数[原来:'
                + thisPlan.limitingConditionFromDaysNumber + ']（单位：天）',
                function (value) {
                    var limitingConditionFromDaysNumber = parseInt(value);
                    if (limitingConditionFromDaysNumber == null && Number.isNaN(limitingConditionFromDaysNumber)) {
                        $.alert('请正确的输入一个数字！');
                        return;
                    }
                    thisPlan.limitingConditionFromDaysNumber = limitingConditionFromDaysNumber;
                    $scope.saveEdit('修改' + thisPlan.maintainName + '所限制的运行天数为:' + limitingConditionFromDaysNumber + '成功');
                }
            );
        };

        // 修改保养计划的保养内容
        $scope.modifyPlansContent = function (index) {
            var thisPlan = $scope.thisDevice.maintainPlans[index];
            app.contentPrompt('更改[' + thisPlan.maintainName + ']的保养内容', function (message) {
                if (!message) {
                    $.alert('保养内容不能为空');
                }
                thisPlan.maintainContent = message;
                $scope.saveEdit('修改[' + thisPlan.maintainName + ']的保养内容成功');
            }, thisPlan.maintainContent);
        };

        // 修改设备详情/描述
        $scope.modifyDeviceDiscription = function () {
            app.contentPrompt('更改[' + $scope.thisDevice.deviceName + ']的详情/描述', function (message) {
                if (!message) {
                    $.alert('详情/描述内容不能为空');
                }
                $scope.thisDevice.deviceDiscription = message;
                $scope.saveEdit('修改[' + $scope.thisDevice.deviceName + ']的详情/描述内容成功');
            }, $scope.thisDevice.deviceDiscription);
        };

        // 修改设备备忘录
        $scope.modifyDeviceMemorandum = function () {
            app.contentPrompt('更改[' + $scope.thisDevice.deviceName + ']的备忘录内容', function (message) {
                if (!message) {
                    $.alert('详情/描述内容不能为空');
                }
                $scope.thisDevice.memorandum.content = message;
                $scope.saveEdit('修改[' + $scope.thisDevice.deviceName + ']的备忘录内容成功');
            }, $scope.thisDevice.memorandum.content);
        };

        // 修改设备名称
        $scope.modifyDeviceName = function () {
            $.prompt('输入新的设备名称', function (name) {
                if (!name) {
                    $.alert('设备名称不能为空！');
                    return;
                }
                $scope.thisDevice.deviceName = name;
                $scope.saveEdit('修改设备名成功');
            })
        };

        // 删除特殊保养
        $scope.removeSpecialMaintain = function (index) {
            var thisPlan = $scope.thisDevice.maintainPlans[index];
            if (!thisPlan) {
                return;
            }
            if (index <= 2) {
                $.toast('该保养非特殊保养，不能删除！', 1000, 'toastToBot');
                return;
            }
            $.confirm('确认删除保养[' + thisPlan.maintainName + ']?', function () {
                $scope.thisDevice.maintainPlans.splice(index, 1);
                $scope.saveEdit('删除成功！');
            });
        };

        // 添加特殊保养
        $scope.addSpecialMaintain = function () {
            $.prompt('输入欲添加的保养计划的名字', function (name) {
                if (name == '') {
                    $.alert('保养计划名不能为空！');
                    return;
                }
                var maintain = new MaintainPlan({
                    maintainName: name,
                    maintainContent: '特殊保养计划',
                    limitingConditionFromRunningDuration: 1,
                    limitingConditionFromDaysNumber: 1
                });
                maintain.takeMaintain($scope.thisDevice, '特殊设备初始化');
                $scope.thisDevice.maintainPlans.push(
                    maintain
                );
                $scope.saveEdit('添加特殊保养[' + name + '成功]');
            });
        };

        // 保存设备修改
        $scope.saveEdit = function (msg) {
            deviceService.updateDevice($scope.thisDevice, function (message) {
                $scope.reloadDevices();
                $scope.$digest();
                if (msg) {
                    $.toast(msg, 1000, 'toastToBot');
                }
            });
        };
    });
});